'''
@Author: your name
@Date: 2020-07-03 12:49:56
LastEditTime: 2020-09-17 10:40:33
LastEditors: Please set LastEditors
@Description: In User Settings Edit
@FilePath: /mtl-text-recognition/config/convert_txt.py
'''
import os
import re


def open_txt(file_name):
    with open(file_name, 'r') as f:
        try:
            line = f.readline()
            while line:
                yield line.strip()
                line = f.readline()
        except:
            print('No value')


def conver_label(gt):
    cn = ['！', '（', '）', '：', ',', '．', '［', '］', '；', '％', '０', '１', '２', '３', '４', '５', '６', '７', '８', '９', '％', '－',
          'Ａ', 'Ｂ', 'Ｃ', 'Ｄ',
          'Ｅ',
          'Ｆ', 'Ｇ', 'Ｈ', 'Ｉ', 'Ｊ', 'Ｋ', 'Ｌ', 'Ｍ', 'Ｎ', 'Ｏ', 'Ｐ', 'Ｑ', 'Ｒ', 'Ｓ', 'Ｔ', 'Ｕ', 'Ｖ', 'Ｗ', 'Ｘ', 'Ｙ', 'Ｚ', 'ａ',
          'ｂ',
          'ｃ', 'ｄ', 'ｅ', 'ｆ', 'ｇ', 'ｈ', 'ｉ', 'ｊ', 'ｋ', 'ｌ', 'ｍ', 'ｎ', 'ｏ', 'ｐ', 'ｑ', 'ｒ', 'ｓ', 'ｔ', 'ｕ', 'ｖ', 'ｗ', 'ｗ',
          'ｙ',
          'ｚ', '￥', ' ']
    en = ['!', '(', ')', ':', '，', '.', '[', ']', ';', '%', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '%', '-',
          'A', 'B', 'C', 'D',
          'E',
          'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
          'b',
          'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
          'y',
          'z', '¥', '']
    gt = ''.join(gt.split()).strip()
    gt_tmp = list(gt)

    for i in range(len(gt_tmp)):
        c = gt_tmp[i]
        if c in cn:
            n = cn.index(c)
            gt_tmp[i] = en[n]

    # for i in range(len(gt_tmp)):
    #     for c in range(len(cn)):
    #         if gt_tmp[i] == cn[c]:
    #             gt_tmp[i] = en[c]

    gt_tmp = ''.join(gt_tmp)
    '''
    for i in range(len(gt_tmp)):
        if c in cn:
            j = cn.index(c)
            gt_tmp[i] = en[j]
    '''
    return gt_tmp

old_dict = [str(line) for line in open_txt('/data1/lhw/workspace/mtl-text-recognition/config/dict/cleaned_all_dict.txt')]
# path = '/data2/code-jin/sohunews/'
# out_path = './sohunews/'

# file_list = os.listdir(path)


# def func(i):
#     mc_set = []
#     file = file_list[i].strip()
#     file_path = os.path.join(path, file)
#     print(file_path)
#     out_file = open(os.path.join(out_path, file), 'w')
#     for text in open_txt(file_path):
#         line = conver_label(text.strip())
#         list_line = list(line.strip())
#         mc_set = list(set(list(list_line) + mc_set))
#         out_file.writelines(line + '\n')
#     return mc_set


# pool = multiprocessing.Pool(processes=30)
# results = []
# for i in range(len(file_list)):
#     results.append(pool.apply_async(func, (i, )))
# pool.close()
# pool.join()
# out_set = []
# for s in results:
#     for c in s.get():
#         if str(c).strip() not in out_set:
#             out_set.append(str(c).strip())

# charset = open('./sohunews_new_dict.txt', 'w')
# for c in out_set:
#     charset.writelines(str(c).strip() + '\n')

# count_1 = 0
# count_3 = 0
# count_5 = 0
# all_count_1 = 0
# all_count_3 = 0
# all_count_5 = 0
# pre_out = '/data1/lhw/workspace/mtl-text-recognition/saved_models/6-30_VGG_CTC_AT_MixedAR/pre_best_accuracy_new_train_labeled.txt'
# for line in open_txt(pre_out):
#     lines = line.split('\t')
#     path = lines[0].strip()
#     gt = lines[1].strip()
#     pre = lines[2].strip()
#     dis = lines[3].strip()
#     num = path[-5]

#     gt = conver_label(gt)
#     if num == '1':
#         if gt == pre:
#             count_1 += 1
#         all_count_1 += 1
#     if num == '3':
#         if gt == pre:
#             count_3 += 1
#         all_count_3 += 1
#     if num == '5':
#         if gt == pre:
#             count_5 += 1
#         all_count_5 += 1

# print('边距为1的准确率 {}'.format(count_1 / all_count_1 * 100))
# print('边距为3的准确率 {}'.format(count_3 / all_count_3 * 100))
# print('边距为5的准确率 {}'.format(count_5 / all_count_5 * 100))

file = '/data1/lhw/workspace/TextRecognitionDataGenerator/trdg/toy_data/Addr_gsmc_smlt.txt'

new_file = open(
    '/data1/lhw/workspace/TextRecognitionDataGenerator/trdg/toy_data/Addr_gsmc_smlt_new.txt',
    'w')
for line in open_txt(file):
    # print(line)
    path = line.split('\t')[0].strip()
    gt = ''.join(line.split('\t')[1:])
    # gt = line.strip()
    judge = True
    for c in list(gt):
        if c not in old_dict:
            judge = False
            pass
    if judge:
        new_file.writelines(path + '\t' + gt + '\n')
    # gt_tmp = conver_label(gt)
    # if gt_tmp[-1] == '.':
    #     gt_tmp = gt_tmp[:-1]
    # new_file.writelines(gt_tmp + '\n')
    
    # if gt[-1] == '.':
    #     print(gt)
    #     gt = gt[:-1]
    # new_file.writelines(path + '\t' + gt + '\n')
    # if re.search(r'(\.m)|(me)', gt):
    #     print(gt)
    # else:
    #     new_file.writelines(path + '\t' + conver_label(gt).strip() + '\n')
    # print(gt)
    # print(conver_label(gt))
    # if gt.strip() != conver_label(gt):
    #     print(gt)
    #     print(conver_label(gt))
